package com.jdew.thkj.fragment;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.activity.MyFansActivity;
import com.jdew.thkj.adapter.MyFansAdapter;
import com.jdew.thkj.base.BaseFragment;
import com.jdew.thkj.bean.FansBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.view.NetworkProgressDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/4/20 0020.
 */

public class SeniorMemberFragment extends BaseFragment{
    @Bind(R.id.noresult)
    TextView noresult;
    @Bind(R.id.tvRefresh)
    TextView tvRefresh;
    @Bind(R.id.swipe_target)
    RecyclerView recycleView;
    @Bind(R.id.tv_load_prompt)
    TextView tvLoadPrompt;
    private MyFansAdapter adapter;
    private MyFansActivity activity;
    private int page=1;
    private List<FansBean.DataBean> list = new ArrayList<>();
    private List<FansBean.DataBean> list2 = new ArrayList<>();

    @Override
    protected int getContentViewId() {
        return R.layout.fans;
    }

    @Override
    protected void onCreateViewT() {
        activity = (MyFansActivity) getActivity();
        sp = activity.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        adapter = new MyFansAdapter(activity, list2);
        setRcycleview(recycleView, adapter);
    }

    @Override
    public void initData() {
    }

    public void GetUserList() {
        if (null == activity.networkProgressDialog) {
            activity.networkProgressDialog = NetworkProgressDialog.createDialog(activity);
        }
        activity.networkProgressDialog.show();
        String timesamp= RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetUserList(sp.getString("userPhone", ""), "2", page + "", RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"UserPhone"+sp.getString("userPhone", "")+"SpareTwo2"+"PageIndex"+page)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<FansBean>() {
            @Override
            public void onCompleted() {
                 NetworkProgressDialog.DialogDismiss(activity.networkProgressDialog);
            }

            @Override
            public void onError(Throwable e) {
                 NetworkProgressDialog.DialogDismiss(activity.networkProgressDialog);
            }

            @Override
            public void onNext(FansBean bean) {
                 NetworkProgressDialog.DialogDismiss(activity.networkProgressDialog);
                if (bean.getCode().equals("0")) {
                    if (page == 1) {
                        list.clear();
                    }
                    list.addAll(bean.getData());
                    if (bean.getData().size() < 20 && bean.getData().size() > 0) {
                        tvLoadPrompt.setText("无更多数据");
                    } else if (bean.getData().size() >= 20) {
                        tvLoadPrompt.setText("正在加载");
                    } else {
                        tvLoadPrompt.setText("无更多数据");
                    }
                    adapter.setDataList(list);
                } else if (bean.getCode().equals("-1")) {
                    if (list.size() == 0 && page==1) {
                        noresult.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        GetUserList();
    }
}
